<template>
  <div style="display: inline-block;text-align: left;">
    <!-- 修改 - btn -->
    <el-button type="primary" icon="el-icon-edit" circle @click="dialogTableVisible = true"/>
    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" width="900px" title="修改" @close="close">
      <el-form ref="row_data" :model="row_data" :rules="rules" label-position="left" label-width="100px" style="width: 750px; margin-left:50px;">
        <el-form-item :label="'id'" prop="id">
          <el-input v-model="row_data.id" placeholder="id" disabled/>
        </el-form-item>
        <el-form-item :label="'openID'" prop="openid">
          <el-input v-model="row_data.openid" placeholder="openID"/>
        </el-form-item>
        <el-form-item :label="'头像'" prop="avatarUrl">
          <el-alert title="建议图片尺寸为 宽 x 高 px" style="margin-bottom: 10px; line-height: 20px" type="success"/>
          <div class="upload_avatarUrl">
            <el-upload :show-file-list="false" :before-upload="beforeAvatarUpload" :http-request="upload_avatarUrl" class="avatar-uploader" action="">
              <img v-if="row_data.avatarUrl" :src="row_data.avatarUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"/>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item :label="'昵称'" prop="nickName">
          <el-input v-model="row_data.nickName" placeholder="昵称"/>
        </el-form-item>
        <el-form-item :label="'性别'" prop="gender">
          <el-radio v-for="item in select_gender" v-model="row_data.gender" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-form-item>
        <el-form-item :label="'国家'" prop="country">
          <el-input v-model="row_data.country" placeholder="国家"/>
        </el-form-item>
        <el-form-item :label="'城市'" prop="city">
          <el-input v-model="row_data.city" placeholder="城市"/>
        </el-form-item>
        <el-form-item :label="'省份'" prop="province">
          <el-input v-model="row_data.province" placeholder="省份"/>
        </el-form-item>
        <el-form-item :label="'语言'" prop="language">
          <el-input v-model="row_data.language" placeholder="语言"/>
        </el-form-item>
        <el-form-item :label="'商家ID'" prop="merchantID">
          <el-input v-model="row_data.merchantID" placeholder="商家ID"/>
        </el-form-item>
        <el-form-item :label="'创建时间'" prop="createTime">
          <el-date-picker v-model="row_data.createTime" type="datetime" value-format="timestamp" placeholder="选择创建时间"/>
        </el-form-item>
        <el-form-item :label="'状态'" prop="status">
          <el-radio v-for="item in select_status" v-model="row_data.status" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-form-item>
        <el-form-item :label="'电话'" prop="phone">
          <el-input v-model="row_data.phone" placeholder="电话"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">编辑</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import store from '@/store'
import { edit } from '@/api/user/user'
import { client } from '@/utils/alioss'

export default {
  name: 'DragDialogDemo',
  directives: { elDragDialog },
  props: {
    row_data: {
      type: Object,
      default: function() {
        return null
      }
    }
  },
  data() {
    return {
      select_gender: [],
      select_status: [{ value: '0', label: '启用' }, { value: '1', label: '禁用' }],
      rules: {
        id: [
          { required: true, message: 'id不能为空', trigger: 'blur' }
        ],
        openid: [
          { required: true, message: 'openID不能为空', trigger: 'blur' }
        ],
        avatarUrl: [
          { type: 'url', required: true, message: '头像地址不正确', trigger: 'blur' },
          { required: true, message: '头像不能为空', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '昵称不能为空', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '性别不能为空', trigger: 'blur' }
        ],
        country: [
          { required: true, message: '国家不能为空', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '城市不能为空', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '省份不能为空', trigger: 'blur' }
        ],
        language: [
          { required: true, message: '语言不能为空', trigger: 'blur' }
        ],
        merchantID: [
          { required: true, message: '商家ID不能为空', trigger: 'blur' }
        ],
        createTime: [
          { type: 'date', required: true, message: '请选择创建时间', trigger: 'blur' },
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false
    }
  },
  created: function() {
    console.log(this.row_data)
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 1
      if (!isJPG && !isPNG) {
        this.$message.error('上传图片只能是 JPG、PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 1MB!')
      }
      return (isJPG | isPNG) && isLt2M
    },
    upload_avatarUrl(file) {
      const that = this
      const fileName = 'user-user-avatarUrl' + file.file.uid
      client().put(fileName, file.file).then((res) => {
        this.$set(that.row_data, 'avatarUrl', res.url)
        that.$message.success('图片上传成功！')
      }).catch((err) => {
        that.$message.error('图片上传失败，原因：' + err)
      })
    },
    submitForm() {
      this.$refs.row_data.validate(valid => {
        if (valid) {
          const data = this.row_data
          data.createTime = parseInt(data.createTime / 1000)
          edit(store.getters.token, data).then(res => {
            if (res.status) {
              this.$message.success('修改成功！')
              this.close()
            } else {
              this.$message.error('修改失败！原因：' + res.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.$emit('getLists')
      this.dialogTableVisible = false
    }
  }
}
</script>
